package it.uniroma1.di.machinelearning;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import org.apache.log4j.Logger;

public class PrepareData {
	private static Logger log = Logger.getLogger(PrepareData.class);

	public static void main(String[] args) {
		
		String folder = args[0];
		String workFolder = folder + "work/";
		String header = args[1];
		String headlessData = args[2];
		
		log.debug(Arrays.deepToString(args));
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mmss");
		String currentDate = sdf.format(new Date());
		
		String shuffledFileName = workFolder + currentDate + "_shuffle_" + headlessData;
		log.debug("shuffledFileName: " + shuffledFileName);
		
		String returnValue;
		
		String splittedFilesPrefix = workFolder + currentDate + "_splitted";
		
		// Shuffle data
		returnValue = LinuxCommands.shuffle(folder + headlessData, shuffledFileName);
		log.debug(returnValue);
		// Split data
		returnValue = LinuxCommands.split(300000, shuffledFileName, splittedFilesPrefix);
		log.debug(returnValue);
		
		// Copy splitted files
		String trainingDataPath = workFolder + currentDate + "_training.arff";
		String testDataPath = workFolder + currentDate + "_test.arff";
		
		try {
			Files.copy(Paths.get(folder+header), Paths.get(trainingDataPath));
			Files.copy(Paths.get(folder+header), Paths.get(testDataPath));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		
		try {
			OutputStream trainingOutputStream = new FileOutputStream(trainingDataPath,true);
			OutputStream testOutputStream = new FileOutputStream(testDataPath,true);
			Files.copy(Paths.get(splittedFilesPrefix + "aa"),trainingOutputStream);
			Files.copy(Paths.get(splittedFilesPrefix + "ab"),testOutputStream);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		// Run algorithms
		
		StackingRunnable stacking = new StackingRunnable();
		stacking.setTrainingPath(trainingDataPath);
		stacking.setTestPath(testDataPath);
		stacking.setOutputPath(workFolder + currentDate + "stacking.txt");
		Thread stackingThread = new Thread(stacking);
		
		DecisionTreeRunnable decisionTreeRunnable = new DecisionTreeRunnable();
		decisionTreeRunnable.setTrainingPath(trainingDataPath);
		decisionTreeRunnable.setTestPath(testDataPath);
		decisionTreeRunnable.setOutputPath(workFolder + currentDate + "decision_tree.txt");
		Thread decisionTreeThread = new Thread(decisionTreeRunnable);
		
		NaiveBayesRunnable naiveBayesRunnable = new NaiveBayesRunnable();
		naiveBayesRunnable.setTrainingPath(trainingDataPath);
		naiveBayesRunnable.setTestPath(testDataPath);
		naiveBayesRunnable.setOutputPath(workFolder + currentDate + "naive_bayes.txt");
		Thread naiveBayesThread = new Thread(naiveBayesRunnable);
		
		SupportVectorRunnable supportVectorRunnable = new SupportVectorRunnable();
		supportVectorRunnable.setTrainingPath(trainingDataPath);
		supportVectorRunnable.setTestPath(testDataPath);
		supportVectorRunnable.setOutputPath(workFolder + currentDate + "support_vector.txt");
		Thread supportVectorThread = new Thread(supportVectorRunnable);		
		
		stackingThread.start();
		decisionTreeThread.start();
		naiveBayesThread.start();
		supportVectorThread.start();
	}
	
	
}
